/*
 * @Descripttion: 
 * @Author: junhuafly
 * @Date: 2020-03-05 15:57:26
 * @LastEditors: junhuafly
 * @Email: junhuafly@163.com
 * @LastEditTime: 2020-04-21 10:33:48
 */
// pages/mine/attention/index.js
import {
  userAPI,
  globalAPI,
  noteAPI
} from './../../../api/index'
import {
  returnStatus,
  returnStatusToast
} from './../../../utils/index'
import {
  utils
} from '../../../components/lib/utils'
var appInst = getApp();


Page({

  /**
   * 页面的初始数据
   */
  data: {
    moreAttention: false,
    pageNum: 1,
    requestObj: {},
    recommendData: [],
    type: 3, // 1:我的关注，2：获赞，3：粉丝
    requestLikesObj: {},
    pageLikeNum: 1,
    likes: []
  },

  async showMore() {
    // 展示更多推荐关注信息
    await this.getData(5)
    this.setData({
      moreAttention: true
    })
  },
  // 关注或取消关注
  handleFollow({
    target
  }) {
    const {
      info,
      index
    } = target.dataset
    let status = 0
    let followRes = null
    const followCustomerId = info.customerId
    if ([0, 2].includes(info.mutual)) {
      followRes = userAPI.followCustomer({
        followCustomerId
      })
      status = 1
      // 取消关注
    } else {
      followRes = userAPI.unFollowCustomer({
        followCustomerId
      })
      status = 0
    }
    followRes.then(async _ => {
      returnStatusToast(_, () => {
        // // 重新获取数据
        // if (this.data.type === 1) {
        //   this.queryMyFollow()
        // } else if (this.data.type === 3) {
        //   this.queryMyFans()
        // } else{
        //   const temp = `recommendData[${index}].mutual`
        //   this.setData({
        //     [temp]: status
        //   })
        // }
        const temp = `recommendData[${index}].mutual`
        this.setData({
          [temp]: status
        })
      })
    })
  },
  toSearch(){
    wx[this.data.keyword?'redirectTo':'navigateTo']({
      url: '/pages/search/index',
    })
  },
  onClickLeft() {
    this.setData({
      moreAttention: false
    })
  },
  // 获取关注数据
  getData(pageSize = 10) {
    return new Promise(async (resolve, reject) => {
      const {
        keyword,
        noteId,
        replyId
      } = this.data;
      let params = {
        pageNum: this.data.pageNum,
        pageSize
      }
      keyword && (params.keyword = keyword)
      noteId && (params.noteId = noteId)
      replyId && (params.replyId = replyId)
      const _ = await (replyId?noteAPI.queryReplyLikeTop:noteId ? noteAPI.queryLikeList : keyword ? globalAPI.queryCustomerByKey : userAPI.queryCustomerRecommendFollow)(params)
      const res = await returnStatus(_)
      if (res) {
        this.setData({
          recommendData: [...this.data.recommendData, ...res.records],
          pageNum: ++this.data.pageNum
        })
        resolve(res.records.length)
      } else {
        reject(false)
      }
    })
  },
  // 获取获赞数据
  getLikeData() {
    return new Promise(async (resolve, reject) => {
      const _ = await userAPI.queryCustomerLikeMe({
        pageNum: this.data.pageLikeNum,
        pageSize: 10,
      })
      const res = await returnStatus(_)
      if (res) {
        for (const r of res.records) {
          r.createDate = utils.relativeTime(r.createDate)
        }
        this.setData({
          likes: [...this.data.likes, ...res.records],
          pageLikeNum: ++this.data.pageLikeNum
        })
        resolve(res.records.length)
      } else {
        reject(false)
      }
    })
  },
  // 获取我的关注
  queryMyFollow() {
    userAPI.queryCustomerFollow().then(async _ => {
      const res = await returnStatus(_);
      if (res) {
        this.setData({
          follows: res
        })
      }
    })
  },
  queryMyFans() {
    userAPI.queryCustomerFans().then(async _ => {
      const res = await returnStatus(_)
      this.setData({
        fans: res
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function ({
    type,
    key: keyword,
    noteId,
    replyId
  }) {
    type *= 1
    appInst.setTitle(noteId||replyId ? '点赞列表' : keyword ? '搜索用户' : ['', '我的关注', '收到赞', '我的粉丝'][type])
    this.setData({
      type,
      keyword,
      noteId,
      replyId
    })
    if (keyword || noteId||replyId) {
      this.getData()
    }
    if (this.data.type === 3) {
      this.queryMyFans()
    }
    this.setData({
      requestObj: {
        fetchData: (refresh) => {
          refresh ? this.setData({
            pageNum: 1,
            recommendData: []
          }) : ''
          return this.getData()
        },
      },
      requestLikesObj: {
        fetchData: (refresh) => {
          refresh ? this.setData({
            pageLikeNum: 1,
            likes: []
          }) : ''
          return this.getLikeData()
        },
      }
    })
    if (this.data.type === 2) {
      this.getLikeData()
    }
    if (this.data.type === 1) {
      this.queryMyFollow()
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({
      pateNum: 1
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})